package com.ruoyi.web.controller.squad;

import com.alibaba.druid.sql.visitor.functions.If;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.squadweb.domain.SquadjsBroadcastMessage;
import com.ruoyi.squadweb.domain.SquadjsServiceList;
import com.ruoyi.squadweb.dto.RconDTO;
import com.ruoyi.squadweb.mapper.SquadjsBroadcastMessageMapper;
import com.ruoyi.squadweb.mapper.SquadjsServiceListMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/squad/rcon")
public class RconController {
    @Value("${squad.ipAddress}")
    private String url;
    @Autowired
    private SquadjsServiceListMapper squadjsServiceListMapper;
    @Autowired
    private SquadjsBroadcastMessageMapper broadcastMessageMapper;
    /**
     * 警告
     * @param rconDTO
     */
    @PostMapping("/gamer/warn")
    public AjaxResult warnPlayer(@RequestBody RconDTO rconDTO) {
        String url = getURL(rconDTO.getServiceId());
        String response = HttpUtils.sendGet(url + "&cmd=AdminWarnById%20" + rconDTO.getId()+ "%20" + rconDTO.getReason());
        return AjaxResult.success();
    }

    /**
     * 踢出
     * @param rconDTO
     */
    @PostMapping("/gamer/kick")
    public AjaxResult kickPlayer(@RequestBody RconDTO rconDTO) {
        String url = getURL(rconDTO.getServiceId());
        String response = HttpUtils.sendGet(url + "&cmd=AdminKickById%20" + rconDTO.getId()+ "%20" + rconDTO.getReason());
        return AjaxResult.success();
    }
    /**
     * 封禁
     * @param rconDTO
     */
    @PostMapping("/gamer/ban")
    public AjaxResult banPlayer(@RequestBody RconDTO rconDTO) {
        String url = getURL(rconDTO.getServiceId());
        String response = HttpUtils.sendGet(url + "&cmd=AdminBanById%20" + rconDTO.getId()+ "%20"+ rconDTO.getTime()+ "%20" + rconDTO.getReason());
        return AjaxResult.success();
    }

    /**
     * 跳边
     * @param rconDTO
     */
    @PostMapping("/change/side")
    public AjaxResult changePlayerSide(@RequestBody RconDTO rconDTO) {
        String url = getURL(rconDTO.getServiceId());
        String response = HttpUtils.sendGet(url + "&cmd=AdminForceTeamChangeById%20" + rconDTO.getId());
        return AjaxResult.success();
    }

    /**
     * 解散小队
     * @param rconDTO
     */
    @PostMapping("/disband/squad")
    public AjaxResult disbandSquad(@RequestBody RconDTO rconDTO) {
        String url = getURL(rconDTO.getServiceId());
        String response = HttpUtils.sendPost(url+"/rcon/disband_squad",
                "teamID=" + rconDTO.getTeamId() + "&squadID=" + rconDTO.getSquadId());
        return AjaxResult.success();
    }
    /**
     * 获取当前地图
     * @param rconDTO
     */
    @PostMapping("/now/map")
    public AjaxResult getNowMap(@RequestBody RconDTO rconDTO) {
        String url = getURL(rconDTO.getServiceId());
        String response = HttpUtils.sendGet(url+"/data/get_current_map");
        if (StringUtils.isEmpty(response)) {
            throw new BaseException("当前获取地图失败");
        }
        JSONObject jsonObject = JSONObject.parseObject(response);
        return AjaxResult.success(jsonObject.get("current_map"));
    }
    /**
     * 获取下一站地图
     * @param rconDTO
     */
    @PostMapping("/next/map")
    public AjaxResult getNexMap(@RequestBody RconDTO rconDTO) {
        String url = getURL(rconDTO.getServiceId());
        String response = HttpUtils.sendGet(url+"/rcon/get_next_map");
        if (StringUtils.isEmpty(response)) {
            throw new BaseException("当前预设地图失败");
        }
        JSONObject jsonObject = JSONObject.parseObject(response);
        return AjaxResult.success(jsonObject.get("next_layer"));
    }

    /**
     * 更换地图
     * @param rconDTO
     */
    @PostMapping("/update/map")
    public AjaxResult updateMap(@RequestBody RconDTO rconDTO) {
        String url = getURL(rconDTO.getServiceId());
        String response = HttpUtils.sendPost(url+"/rcon/change_layer",
                "layer_name=" + rconDTO.getMapName());
        return AjaxResult.success();
    }

    /**
     * 预设下一张地图
     * @param rconDTO
     */
    @PostMapping("/presets/map")
    public AjaxResult presetsMap(@RequestBody RconDTO rconDTO) {
        String url = getURL(rconDTO.getServiceId());
        String response = HttpUtils.sendPost(url+"/rcon/change_next_layer",
                "layer_name=" + rconDTO.getMapName());
        return AjaxResult.success();
    }

    /**
     * 广播发送
     * @param rconDTO
     */
    @PostMapping("/send/broadcast")
    public AjaxResult sendBroadcast(@RequestBody RconDTO rconDTO) {
        String url = getURL(rconDTO.getServiceId());
        String response = HttpUtils.sendPost(url+"/rcon/send_broadcast",
                "content=" + rconDTO.getMessage());
        SquadjsBroadcastMessage squadjsBroadcastMessage = new SquadjsBroadcastMessage();
        squadjsBroadcastMessage.setMessage(rconDTO.getMessage());
        squadjsBroadcastMessage.setSendName(rconDTO.getSendName());
        //把yyyy-MM-dd hh:mm:ss转换成data类型
        squadjsBroadcastMessage.setTime(DateUtils.dateTime("yyyy-MM-dd hh:mm:ss",rconDTO.getTime()));
        broadcastMessageMapper.insertSquadjsBroadcastMessage(squadjsBroadcastMessage);
        return AjaxResult.success();
    }

    private String getURL(String serviceId) {
        if (StringUtils.isEmpty(serviceId)) {
            throw new BaseException("服务器不存在，请输入正确服务器");
        }
        SquadjsServiceList squadjsServiceList = squadjsServiceListMapper.selectSquadjsServiceListById(Long.parseLong(serviceId));
        if (squadjsServiceList == null || StringUtils.isEmpty(squadjsServiceList.getIp())) {
            throw new BaseException("服务器不存在，请输入正确服务器");
        }
        return "http://api.bctc-squad.cn:8088/api/Rcon.sh?ip="+squadjsServiceList.getIp()+"&port="+squadjsServiceList.getPort()+"&passwd="+squadjsServiceList.getPassword();
    }
}
